package com.dein.music.controller;

import com.dein.music.model.Music;
import com.dein.music.model.MusicPreference;
import com.dein.music.service.MusicPreferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("musicPreference/")
public class MusicPreferenceController {
    private MusicPreferenceService musicPreferenceService;

    @Autowired
    @Qualifier("musicPreferenceService")
    public void setMusicPreferenceService(MusicPreferenceService musicPreferenceService) {
        this.musicPreferenceService = musicPreferenceService;
    }

    @RequestMapping(value = "addPreferenceMusic", method = RequestMethod.POST)
    public
    @ResponseBody
    Object addPreferenceMusic(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "musicId")Long musicId,
            @RequestParam(value = "preference")float preference) {
        Map<String, Object> map = new HashMap<String, Object>();

        boolean result = musicPreferenceService.addPreferenceMusic(userId, musicId, preference);
        map.put("result",result);
        return map;
    }

    @RequestMapping(value = "deletePreferenceMusic", method = RequestMethod.POST)
    public
    @ResponseBody
    Object deletePreferenceMusic(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "musicId")Long musicId) {
        Map<String, Object> map = new HashMap<String, Object>();

        boolean result = musicPreferenceService.deletePreferenceMusic(userId, musicId);
        map.put("result",result);
        return map;
    }

    @RequestMapping(value = "getPreferenceMusicList", method = RequestMethod.POST)
    public
    @ResponseBody
    Object getPreferenceMusicList(
            @RequestParam(value = "userId")Long userId,
            @RequestParam(value = "limit")Integer limit,
            @RequestParam(value = "start")Integer start) {
        List<Music> musicList = new ArrayList<Music>();

        musicList = musicPreferenceService.getPreferenceMusicList(userId,limit,start);
        return musicList;
    }

    @RequestMapping(value = "countPreferenceMusic",method = RequestMethod.POST)
    public @ResponseBody
    Object countPreferenceMusic(@RequestParam(value = "userId")Long userId){
        Integer count = musicPreferenceService.countPreferenceMusic(userId);
        return count;
    }

    @RequestMapping(value = "preferenceMusicExist",method = RequestMethod.POST)
    public @ResponseBody
    Object preferenceMusicExist(@RequestParam(value = "userId")Long userId,
        @RequestParam(value = "musicId")Long musicId){
        Map<String, Object> map = new HashMap<String, Object>();
        boolean result = musicPreferenceService.preferenceMusicExist(userId, musicId);
        if(!result){
            map.put("result", false);
            return map;
        }
        map.put("result", true);
        return map;
    }
}
